#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author: wangxiaoming 2021-11-16
# desc: thermal测试类

import enum
import unittest
import os
import time
import struct
from ctypes import *
from mainunit_common import *
from mainunit_api import *

class testself_btn_drv(unittest.TestCase):
    g_btn_type=BTNERR
    g_btn_val=BSP_OFF
    
    def data_callback(self, btn_type, btn_data):
        print(f'\tbtn_type={btn_type} data={btn_data}')
        self.g_btn_type = btn_type
        self.g_btn_val = btn_data
    
    def error_callback(self, err):
        print(f'\terr={err}')
        
    def testself1_btn_drv_value(self):
        print(f'\t==================================')
        expected=ECODE_OK
        
        ret = btn_drv_init()
        print(f'\tstep. call api: btn_drv_init, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        txt = sp_input(f"\n\tPLEASE INPUT BUTTON KEY[BTN_ECALL:{BTN_ECALL} BTN_CUSTOM:{BTN_CUSTOM} BTN_EVENT:{BTN_EVENT}] TO CONTINUE\n")        
        c_txt = int(txt)
        status = c_int(-1)
        ret = btn_drv_get_status(c_txt, pointer(status))        
        print(f'\tstep. call api: btn_drv_get_status, sp_input param={c_txt} actual value={ret}, expected value={expected}')		
        self.assertEqual(ret, expected)		
        self.assertEqual(BSP_OFF, status.value)
        
        txt = sp_input(f"\n\tPLEASE INPUT BUTTON KEY[BTN_ECALL:{BTN_ECALL} BTN_CUSTOM:{BTN_CUSTOM} BTN_EVENT:{BTN_EVENT}] AND THEN PRESS BUTTON TO CONTINUE\n")        
        time.sleep(5)
        c_txt = int(txt)
        status = c_int(-1)
        ret = btn_drv_get_status(c_txt, pointer(status))        
        print(f'\tstep. call api: btn_drv_get_status, actual value={ret}, expected value={expected}')		
        self.assertEqual(ret, expected)		
        self.assertEqual(BSP_ON, status.value)
        
        txt = sp_input("\n\tPLEASE RELEASE BUTTON ANY KEY TO CONTINUE\n")
        
        ret = btn_drv_uinit()
        print(f'\tstep. call api: btn_drv_uinit, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)		
        print('\t**********************************')
        
    def testself2_btn_drv_start(self):
        print(f'\t==================================')
        expected=ECODE_OK
        erro_cb_obj = ErrorCallBack(self.error_callback)
        data_cb_obj = BTNDataCallBack(self.data_callback)
        
        ret = btn_drv_init()
        print(f'\tstep. call api: btn_drv_init, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
        
        ret = btn_drv_start(data_cb_obj,erro_cb_obj)
        print(f'\tstep. call api: btn_drv_start, actual value={ret}, expected value={expected}')		
        self.assertEqual(ret, expected)
        
        txt = sp_input(f"\n\tPLEASE PRESS ECALL BUTTON KEY TO CONTINUE\n")        
        time.sleep(5)    
        self.assertEqual(BTN_ECALL, self.g_btn_type)
        self.assertEqual(BSP_ON, self.g_btn_val)
        txt = sp_input(f"\n\tPLEASE RELEASE ECALL BUTTON KEY TO CONTINUE\n")        
        
        ret = btn_drv_stop()
        print(f'\tstep. call api: btn_drv_stop, actual value={ret}, expected value={expected}')		
        self.assertEqual(ret, expected)
        
        ret = btn_drv_uinit()
        print(f'\tstep. call api: btn_drv_uinit, actual value={ret}, expected value={expected}')
        self.assertEqual(ret, expected)
		
        print('\t**********************************')    